import router from "./router";
import nProgress from "nprogress";
import 'nprogress/nprogress.css'
import store from "./store";


//白名单
const whiteList = ['/login', '/404']
router.beforeEach(async (to, from, next) => {
  nProgress.start()
  if (store.getters.token) {

    if (to.path === '/login') {
      //如果有token并且取得是登录页那么跳转到主页
      next('/')
      nProgress.done()
    } else {
      //判断是否获取过资料
      if (!store.getters.userId) {

        await store.dispatch('user/getUserInfo')
        //如果获取到未登录 那么返回login

        //获取菜单信息
        await store.dispatch('user/getMenuList')
        next(to.path)
      } else {
        next()
      }
    }
  } else {
    //没有token
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login') //跳转到登录页 
      nProgress.done() //手动关闭进度条 
    }

  }
})

router.afterEach(() => {
  nProgress.done()
})